Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Xml
Imports System.ComponentModel
Imports C4F.DevKit.WebServices.Datasets

Namespace C4F.DevKit.WebServices
	''' <summary>
	''' Exposes the functionality to get RSS feed items for requested URL
	''' </summary>
	<ToolboxItem(True)> _
	Public Class Rss
		Inherits C4FWebServicesBase
		#Region "Public functions"

		''' <summary>
		''' Gets Rss feed items for requested URL.
		''' </summary>
		''' <param name="feedUrl">Url of the RSS feed</param>
		''' <param name="publishedAfterDate">Optional parameter. If specified, only items published after 
		''' this date will be returned</param>
		''' <returns>List of feed items matching input criteria</returns>
		''' <exception cref="ArgumentNullException">The feedUrl is not specified</exception>
		Public Function GetRssFeed(ByVal feedUrl As String, ByVal publishedAfterDate As Nullable(Of DateTime)) As List(Of RssFeedData)
			If String.IsNullOrEmpty(feedUrl) Then
				Throw New ArgumentNullException("feedUrl")
			End If

			Dim responseData As String = MyBase.GetQueryResponse(feedUrl)
			Dim rssFeedDocument As XmlDocument = New XmlDocument()
			rssFeedDocument.LoadXml(responseData)

			' Retrieve all item nodes
			Dim rssFeedItems As XmlNodeList = rssFeedDocument.DocumentElement.SelectSingleNode("channel").SelectNodes("item")
			Dim feedData As RssFeedData
			Dim feedItems As List(Of RssFeedData) = New List(Of RssFeedData)()

			For Each rssFeedItemNode As XmlNode In rssFeedItems
				feedData = New RssFeedData()
				feedData.Title = rssFeedItemNode.SelectSingleNode("title").InnerText
				feedData.Url = rssFeedItemNode.SelectSingleNode("link").InnerText
				feedData.Description = rssFeedItemNode.SelectSingleNode("description").InnerText
				If Not rssFeedItemNode.SelectSingleNode("pubDate") Is Nothing Then
					feedData.PublishedOnDate = DateTime.Parse(rssFeedItemNode.SelectSingleNode("pubDate").InnerText, Nothing, System.Globalization.DateTimeStyles.RoundtripKind)
				End If
				If Not rssFeedItemNode.SelectSingleNode("author") Is Nothing Then
					feedData.Author = rssFeedItemNode.SelectSingleNode("author").InnerText
				End If

				If (Not publishedAfterDate.HasValue) OrElse feedData.PublishedOnDate > publishedAfterDate.Value Then
					feedItems.Add(feedData)
				End If
			Next rssFeedItemNode

			Return feedItems
		End Function

		''' <summary>
		''' Gets Rss feed items for requested URL.
		''' </summary>
		''' <param name="feedUrl">Url of the RSS feed</param>
		''' <param name="publishedAfterDate">Optional parameter. If specified, only items published after 
		''' this date will be returned</param>
		''' <returns>Dataset containing list of feed items matching input criteria</returns>
		''' <exception cref="ArgumentNullException">The feedUrl is not specified</exception>
		Public Function GetRssFeed_DS(ByVal feedUrl As String, ByVal publishedAfterDate As Nullable(Of DateTime)) As DS_Rss
			Dim feedItems As List(Of RssFeedData) = GetRssFeed(feedUrl, publishedAfterDate)
			Dim dataset As DS_Rss = New DS_Rss()
			For Each feedItem As RssFeedData In feedItems
				dataset.RssFeedData.AddRssFeedDataRow(feedItem.Title, feedItem.Url, feedItem.Description, feedItem.PublishedOnDate, feedItem.Author)
			Next feedItem
			Return dataset
		End Function
		#End Region
	End Class

	''' <summary>
	''' Represents an RSS feed item
	''' </summary>
	Public Class RssFeedData
		#Region "Private Members"
		''' <summary>
		''' Title of the item
		''' </summary>
		Private title_Renamed As String
		''' <summary>
		''' URL to item details
		''' </summary>
		Private url_Renamed As String
		''' <summary>
		''' Description of the item
		''' </summary>
		Private description_Renamed As String
		''' <summary>
		''' Date and time when published
		''' </summary>
		Private publishedOnDate_Renamed As DateTime
		''' <summary>
		''' Author name
		''' </summary>
		Private author_Renamed As String
		#End Region

		#Region "Public Properties"
		''' <summary>
		''' Title of the item
		''' </summary>
		Public Property Title() As String
			Get
				Return Me.title_Renamed
			End Get
			Set(ByVal value As String)
				Me.title_Renamed = value
			End Set
		End Property
		''' <summary>
		''' URL to item details
		''' </summary>
		Public Property Url() As String
			Get
				Return Me.url_Renamed
			End Get
			Set(ByVal value As String)
				Me.url_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Description of the item
		''' </summary>
		Public Property Description() As String
			Get
				Return Me.description_Renamed
			End Get
			Set(ByVal value As String)
				Me.description_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Date and time when published
		''' </summary>
		Public Property PublishedOnDate() As DateTime
			Get
				Return Me.publishedOnDate_Renamed
			End Get
			Set(ByVal value As DateTime)
				Me.publishedOnDate_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Name of the author
		''' </summary>
		Public Property Author() As String
			Get
				Return Me.author_Renamed
			End Get
			Set(ByVal value As String)
				Me.author_Renamed = value
			End Set
		End Property
		#End Region
	End Class
End Namespace